package com.wanli.agent.mine;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.google.gson.Gson;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseFragment;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.BaseResponseBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.event.UpDateNameEvent;
import com.wanli.agent.homepage.ExtensionCodeActivity;
import com.wanli.agent.login.LoginActivity;
import com.wanli.agent.mine.model.MineModelImpl;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.utils.VersionUtil;
import com.wanli.agent.widget.CommonNoTitleDialog;
import com.wanli.agent.widget.ViewLoading;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import ch.ielse.view.SwitchView;

/**
 * 我的
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.iv_wx_type)
    ImageView ivWxType;
    @BindView(R.id.ll_my_settle_price)
    LinearLayout llMySettlePrice;
    @BindView(R.id.switchview)
    SwitchView switchview;
    @BindView(R.id.ll_bg_launch)
    LinearLayout llBgLaunch;
    @BindView(R.id.ll_feekback)
    LinearLayout llFeekback;
    @BindView(R.id.ll_about_us)
    LinearLayout llAboutUs;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    @BindView(R.id.ll_outLogin)
    LinearLayout llOutLogin;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.ll_extension_code)
    LinearLayout ll_extension_code;
    private Unbinder unbinder;
    private UserInfoBean userInfoBean;
    private CommonNoTitleDialog.Builder dialogBuild;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        registerEventBus();
        return view;
    }

    public void initView() {
        tvVersion.setText("V" + VersionUtil.packageName(mActivity));
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);

//        //标准版服务商不显示拓展码哈
//        int channel_type = userInfoBean.getData().getChannel_type();
//
//        if (channel_type==2){
//            //智能版
//            ll_extension_code.setVisibility(View.VISIBLE);
//        }

        int id = userInfoBean.getData().getId();
        tvName.setText(userInfoBean.getData().getName());
        tvMobile.setText(userInfoBean.getData().getMobile());
        if (!TextUtils.isEmpty(userInfoBean.getData().getWechat_openid())) {
            ivWxType.setImageResource(R.mipmap.ic_wechat1);
        } else {
            ivWxType.setImageResource(R.mipmap.ic_wechat2);
        }

        if ("true".equals(SPManager.getInstance().getSpeechSwitch())) {
            switchview.setOpened(true);
        } else {
            switchview.setOpened(false);
        }

        switchview.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(SwitchView view) {
                switchview.setOpened(true);
                SPManager.getInstance().saveSpeechSwitch("true");
            }

            @Override
            public void toggleToOff(SwitchView view) {
                switchview.setOpened(false);
                SPManager.getInstance().saveSpeechSwitch("false");
            }
        });
    }

    @Subscribe
    public void onUpDateNameEvent(UpDateNameEvent event) {
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        tvName.setText(userInfoBean.getData().getName());
    }

    @OnClick({R.id.ll_header, R.id.tv_name, R.id.tv_mobile, R.id.ll_my_settle_price, R.id.ll_bg_launch, R.id.ll_feekback, R.id.ll_about_us, R.id.ll_setting, R.id.ll_outLogin, R.id.ll_extension_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_header:
            case R.id.tv_name:
            case R.id.tv_mobile:
                if (userInfoBean == null) {
                    return;
                }
                Intent intent = new Intent(mActivity, UpdateNameActivity.class);
                intent.putExtra("name", userInfoBean.getData().getName());
                startActivity(intent);
                break;
            case R.id.ll_my_settle_price:
                skipToActivity(MySettlePriceActivity.class);
                break;
            case R.id.ll_bg_launch:
                skipToActivity(LaunchManageActivity.class);
                break;
            case R.id.ll_feekback:
                skipToActivity(FeedBackActivity.class);
                break;
            case R.id.ll_about_us:
                skipToActivity(AboutUsActivity.class);
                break;
            case R.id.ll_setting:
                skipToActivity(SettingActivity.class);
                break;
            case R.id.ll_outLogin:
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("是否退出登录?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    outLogin();
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
                break;
            case R.id.ll_extension_code:
                skipToActivity(ExtensionCodeActivity.class);
                break;
        }
    }

    /**
     * 退出登录
     */
    public void outLogin() {
        ViewLoading.showProgress(mActivity);
        //解绑DeviceId
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        new MineModelImpl().requestUnBindDeviceId(pushService.getDeviceId(), new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
        //退出登录
        new MineModelImpl().requestLoginOut(userInfoBean.getData().getId() + "", new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
        SPManager.getInstance().logOffRemove();
        Intent intent = new Intent(mActivity, LoginActivity.class);
        startActivity(intent);
        mActivity.finish();
    }

}
